<?php

namespace App\Http\Controllers\Guard\SerialNumber;

use App\Exports\SerialNumberExport;
use App\Http\Controllers\Common\GuardController;
use App\Models\SerialNumber\SerialNumber;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class SerialNumberController extends GuardController {

    public function lst(Request $request) {
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',20);
        $conditions = $request->input();
        $records = (new SerialNumber())->getLst($conditions)
            ->with(['getDriverInfo:id,user_id,name,id_card','getUserInfo:id,active_time,phone'])->paginate($pageSize,['*'],'page',$page);
        return responseSuccess($records->items(),$records->total());
    }

    public function previewGenerate(Request $request) {
        $type = $request->input('type','D');
        $count = $request->input('count',20);
        $records = [];
        foreach (range(1, $count) as $id) {
            $serial_number = SerialNumber::generateSerialNumber($type);
            $records[] = compact('id','serial_number');
        }
        return responseSuccess($records,count($records));
    }

    public function generate(Request $request) {

        //今日生成的序列号数量
       /* $todayCount = (new SerialNumber())->getLst(['createdAt'=>date('Y-m-d')])->count();
        $maxCount = 20;
        if($todayCount>=$maxCount){
            return responseFail('一天最多生成'.$maxCount.'个序列号！');
        }*/

        $type = $request->input('type','D');
        $count = $request->input('count',20);
        foreach (range(1, $count) as $item) {
            $serial_number = SerialNumber::generateSerialNumber($type);
            SerialNumber::create(compact('serial_number','type'));
        }
        return responseSuccess();
    }

    public function exportXls(Request $request) {
        $conditions = $request->input();
        $conditions['orderBy']['col'] = 'id';
        $conditions['orderBy']['sort'] = 'asc';
        return Excel::download(new SerialNumberExport($conditions), 'serialNumber.xlsx');
    }

    public function changeStatus(Request $request) {
        $status = $request->input('status',1);

        if($request->has('serialNumber')){
            $serialNumber = $request->input('serialNumber');
            return $this->changeCol(SerialNumber::class, ['serial_number'], [$serialNumber], ['status'], [$status]);
        }


        if($request->has('ids')){
            $ids = $request->input('ids');
            SerialNumber::whereIn('id',$ids)->update(compact('status'));
            return responseSuccess();
        }else{
            $id = $request->input('id',0);
            return $this->changeCol(SerialNumber::class, ['id'], [$id], ['status'], [$status]);
        }
    }

    public function addBeizhu(Request $request) {
        $id = $request->input('id',0);
        $beizhu = $request->input('beizhu');
        return $this->changeCol(SerialNumber::class, ['id'], [$id], ['beizhu'], [$beizhu]);
    }


}
